# Updated 2012/01/09
# DO NOT USE THIS
# This program has some problems. It confused the freqOfTermsInQueries and the freqOfTermsInCollection


from __future__ import division

print "begin..."

# This part of script is used to combine the term freq in the query trace into the main machine learned file
inputFileName = "/data5/team/obukai/the_new_trip_of_feature_generation/gov2ClearYourMindAndDoItAgain/freqOfTermsInQueries.txt"
inputFileHandler = open(inputFileName,"r")

# Questions?
# Whether this can truely cover all the training instances?
queryTermFreqDict = {}


for line in inputFileHandler.readlines():
    queryTerm = line.strip().split(" ")[0]
    queryTermFreqInCollection = int( line.strip().split(" ")[1] )
    # print queryTerm,queryTermFreqInCollection
    if queryTerm not in queryTermFreqDict:
        queryTermFreqDict[queryTerm] = queryTermFreqInCollection

print "len(queryTermFreqDict):",len(queryTermFreqDict)
print "queryTermFreqDict['finishers']:",queryTermFreqDict["finishers"]

inputFileName2 = "/data5/team/obukai/the_new_trip_of_feature_generation/gov2/wholeTrainingFileTempleteGov2V1_phase0_posting_oriented.txt.input"
inputFileHandler2 = open(inputFileName2,"r")

outputFileName = "/data5/team/obukai/the_new_trip_of_feature_generation/gov2/wholeTrainingFileTempleteGov2V1_phase0_posting_oriented_term_freq_inCollection_added.txt.input"
outputFileHandler = open(outputFileName,"w")

for index,line in enumerate( inputFileHandler2.readlines() ):
    # print "originalLine:",line
    lineElements = line.strip().split(" ")
    queryTerm = lineElements[2]
    if queryTerm in queryTermFreqDict:
        outputLine = ""
        
        # step1: original elements from the line
        for element in lineElements[:-1]:
            outputLine += element + " "
        
        # step2: new term freq in the collection from the line
        outputLine += str( queryTermFreqDict[queryTerm] ) + " "
        
        # step3: add the final label on it
        outputLine += lineElements[-1]
        
        # step4: write the line to the output file
        outputFileHandler.write(outputLine + "\n")
        # print "outputLine:",outputLine
    else:
        print "ERROR: queryTerm:",queryTerm
        exit(1)
        
inputFileHandler.close()
inputFileHandler2.close()
outputFileHandler.close()
   
print "end."     
        
        
        
        
        
        
        
        
        